package com.atkid.boot.service.impl;

import com.atkid.boot.entity.Club;
import com.atkid.boot.entity.Manager;
import com.atkid.boot.entity.Student;
import com.atkid.boot.entity.vo.StuVo;
import com.atkid.boot.mapper.ManagerMapper;
import com.atkid.boot.service.ClubService;
import com.atkid.boot.service.ManagerService;
import com.atkid.boot.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kid
 * @since 2021-12-07
 */
@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {

  @Autowired
  StudentService studentService;
  @Autowired
  ManagerService managerService;
  @Autowired
  ClubService clubService;


  @Override
  public Page<StuVo> getStudents(Long current,Long size,int cid){
    List<StuVo> stuVos = new ArrayList<>();
    Page<StuVo> pageVo = new Page<>(current,size);
    Page<Student> stuPage = new Page<>(current,size);
    QueryWrapper<Student> qw = new QueryWrapper<>();
    qw.eq("club1_id",cid).or().eq("club2_id",cid);
    studentService.page(stuPage,qw);
    List<Student> studentList = stuPage.getRecords();
    for (Student student : studentList)
    {
      StuVo tmp = new StuVo();
      tmp.setName(student.getName());
      tmp.setClassName(student.getClassName());
      tmp.setDepartment(student.getDepartment());
      tmp.setId(student.getId());
      tmp.setPhone(student.getPhone());
      Integer club1Id = student.getClub1Id();
      Integer club2Id = student.getClub2Id();
      if (club1Id!=null && club1Id == cid)
      {
        Club club = clubService.getById(cid);
        tmp.setClub(club.getName());
        tmp.setTime(student.getTime1());
      }
      if (club2Id!=null && club2Id == cid)
      {
        Club club = clubService.getById(cid);
        tmp.setClub(club.getName());
        tmp.setTime(student.getTime2());
      }
      if (student.getSex()==1)
      {
        tmp.setSex("男");
      }
      if (student.getSex()==0)
      {
        tmp.setSex("女");
      }
      stuVos.add(tmp);
    }
    pageVo.setRecords(stuVos);
    pageVo.setTotal(stuPage.getTotal());
    pageVo.setCurrent(current);
    pageVo.setSize(size);
    pageVo.setPages(stuPage.getPages());
    return pageVo;
  }

  @Override
  public Club getClubByManager(int id){
    Manager manager = managerService.getById(id);
    int cid = manager.getClubId();
    return clubService.getById(cid);
  }
}
